package com.claims;

import java.util.concurrent.atomic.AtomicLong;

/**
 * Created by 峥桂 on 2017/6/6.
 */
public class CASUtils {
    private Long currLong = 1l;
    private static AtomicLong atomicLong = new AtomicLong(1l);

    public boolean updateStateLong(Long varl) {
//        System.out.print("===>>"+atomicLong);
        return atomicLong.compareAndSet(getCurrLong(), varl);
    }

    private static AtomicLong atomicLongsync = null;

    public boolean updateStateLongSnc(Long varl) {
//        System.out.print("===>>"+atomicLong);
        return getAtomicLong().compareAndSet(getCurrLong(), varl);
    }

    public AtomicLong getAtomicLong() {
        if (atomicLongsync == null) {
            synchronized (AtomicLong.class) {
                System.out.println(atomicLongsync);
               if(atomicLongsync==null){
                    atomicLongsync = new AtomicLong(getCurrLong());
               }
           }
        }
        return atomicLongsync;
    }

    public void setCurrLong(Long currLong_) {
        currLong = currLong_;
    }

    public Long getCurrLong() {
        return currLong;
    }

    public void cleanAtomicLong(){
        atomicLongsync=null;
    }
}
